package com.dpxm.zdbdg.charte.sand.service;

import com.dpxm.zdbdg.charte.sand.dao.SanDAllDao;
import com.dpxm.zdbdg.charte.sand.dao.SanDDao;
import com.dpxm.zdbdg.charte.sand.vo.QuerySanDReq;
import com.dpxm.zdbdg.charte.sand.vo.SanDChaiFen;
import com.dpxm.zdbdg.charte.sand.vo.SanDVO;
import com.dpxm.zdbdg.charte.sand.vo.SandRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vehiclebase.enums.QueryEnum;
import vehiclebase.utils.BusinessException;
import vehiclebase.utils.ToolsUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ChartSandService {
    @Autowired
    private SanDAllDao sanDAllDao;
    @Autowired
    private SanDDao sanDDao;

    public SandRes coldHot( QuerySanDReq req) {
        SandRes sandRes = new SandRes();
        if(ToolsUtils.isEmpty(req.getQishu())){
            sandRes.setErrorRes("期数不允许为空！");
            return sandRes;
            // throw new BusinessException("参数不能为空" );
        }
        //重号次数的号码是独立的 单独查询。
        if(ToolsUtils.notEmpty(req.getChongHaoNubm())){
            //查询期数内的重号次数情况。
            List<String> sanChongHao = sanDDao.QueryChongMessage(req.getChongHaoNubm(),req.getNubm(),req.getQishu());
            List<String> lists = new ArrayList<>();
            if(ToolsUtils.notEmpty(sanChongHao) && ToolsUtils.notEmpty(req.getShaNubm())){ //杀号码
                List<String> shaHaolist = new ArrayList<>();
                for (int j=0 ;j<sanChongHao.size() ; j++){
                    if( ToolsUtils.notEmpty(sanChongHao.get(j))){
                        int faleNubm=0;
                        for(int i=0 ;i<req.getShaNubm().size() ; i++){
                            if(!(sanChongHao.get(j).contains(req.getShaNubm().get(i)))){  //表示含有杀号码的号码 需要删除
                                faleNubm=faleNubm+1;
                            }
                        }
                        if(faleNubm == req.getShaNubm().size()){
                            Boolean falg = resOddOrOpen(sanChongHao.get(j),req);
                            if(falg){
                                shaHaolist.add(sanChongHao.get(j)); //得到号码
                            }
                        }
                    }
                }
                lists= shaHaolist.stream().distinct().collect(Collectors.toList());

            }else{
                List<String> noShaHaolist = new ArrayList<>();
                for (int j=0 ;j<sanChongHao.size() ; j++){
                    if( ToolsUtils.notEmpty(sanChongHao.get(j))){
                        Boolean falg = resOddOrOpen(sanChongHao.get(j),req);
                        if(falg){
                            noShaHaolist.add(sanChongHao.get(j)); //得到号码
                        }
                    }
                }
                lists= noShaHaolist.stream().distinct().collect(Collectors.toList());
            }

            sandRes.setChongList(lists);
        }
        //查询3d所有的号码 供后续使用
        List<String> sanDAll= sanDAllDao.QueryMessage();
        //查询最近期数的号码 供后续使用
        List<String> sanDkai = sanDDao.QueryMessage(req.getQishu());
        //最近的号码需要先去重复
        List<String> cotList=sanDkai.stream().distinct().collect(Collectors.toList());
        // 得到期数内没有开过的号码
        List<String> cotNumbs = sanDAll.stream().filter(a->!cotList.contains(a)).collect(Collectors.toList());
        List<String> list = new ArrayList<>();
        if(ToolsUtils.notEmpty(req.getShaNubm())){  //杀号码
            List<String> shaHaolist = new ArrayList<>();
            for (int j=0 ;j<cotNumbs.size() ; j++){
                if( ToolsUtils.notEmpty(cotNumbs.get(j))){
                    int faleNubm=0;
                    for(int i=0 ;i<req.getShaNubm().size() ; i++){
                        if(!(cotNumbs.get(j).contains(req.getShaNubm().get(i)))){  //表示含有杀号码的号码 需要删除
                            faleNubm=faleNubm+1;
                        }
                    }
                    if(faleNubm == req.getShaNubm().size()){
                        Boolean falg = resOddOrOpen(cotNumbs.get(j),req);
                        if(falg){
                            shaHaolist.add(cotNumbs.get(j)); //得到号码
                        }
                    }
                }
            }
            list= shaHaolist.stream().distinct().collect(Collectors.toList());
        }else {
            List<String> noShaHaolist = new ArrayList<>();
            for (int j=0 ;j<cotNumbs.size() ; j++){
                if( ToolsUtils.notEmpty(cotNumbs.get(j))){
                    Boolean falg = resOddOrOpen(cotNumbs.get(j),req);
                    if(falg){
                        noShaHaolist.add(cotNumbs.get(j)); //得到号码
                    }
                }
            }
            list= noShaHaolist.stream().distinct().collect(Collectors.toList());
        }


        List<String> noNumbList=new ArrayList<>(); //无对子的数据集合
        List<String> yesNumbList=new ArrayList<>(); //有对子的数据集合
        List<String> numbfonum=new ArrayList<>();//有某个数的集合
        //if(QueryEnum.FALG_DUIZI_NO.val().equals(req.getFalg())){ //无对子的数据
        for (int i=0 ;i<list.size() ; i++){
            if( ToolsUtils.notEmpty(list.get(i))){
                Boolean st = isUnique(list.get(i));
                if(!st){
                    noNumbList.add(list.get(i)); //无对子的数据
                }else {
                    yesNumbList.add(list.get(i)); //只有对子的数据
                }
            }
        }
        sandRes.setNoNumbList(noNumbList);
        sandRes.setYesNumbList(yesNumbList);
        // }
       /*if(QueryEnum.FALG_DUIZI_YES.val().equals(req.getFalg())){ //只有对子的数据
            for (int j=0 ;j<cotNumb.size() ; j++){
                if( ToolsUtils.notEmpty(cotNumb.get(j))){
                    Boolean st = isUnique(cotNumb.get(j));
                    if(st){
                        yesNumbList.add(cotNumb.get(j));
                    }
                }
            }
       }*/
        if(ToolsUtils.notEmpty(req.getNubm())){ //必开的某个数的集合
            for (int j=0 ;j<list.size() ; j++){
                if( ToolsUtils.notEmpty(list.get(j))){
                    Boolean st =list.get(j).contains(req.getNubm());
                    if(st){
                        numbfonum.add(list.get(j));
                    }
                }
            }
        }
        sandRes.setNumbfonum(numbfonum);
        sandRes.setHotNumber(list);

        return sandRes;
    }
    Map<Character,Character> map=new HashMap<>();
    public boolean isUnique (String string) {
        /**
         * 判断字符串是否含有重复字符：
         *     1.首先将字符串转成字符数组
         *     2.利用 indexof 与 lastindexof 判断字符是否重复
         */
        char[]arr = string.toCharArray();
        for(char ch:arr) {
            if (string.indexOf(ch) != string.lastIndexOf(ch)) {
                // System.out.println("字符串含有重复字符");
                return true;
            }
        }
        return false;
    }


    /**
     * @Description: 3D存最新一期的号码
     * @Param: qihao 期号
     * @Param: nubm 开奖号
     * @Param: testnubm 试机号
     * @Param: createdate 时间
     * @Date: 2022年5月27日16:06:58
     */
    public String saveSanD (String qihao,String nubm,String testnubm,String createdate) throws Exception {
        SanDVO sanDVO = new SanDVO();
        sanDVO.setQihao(qihao);
        sanDVO.setNubm(nubm);
        sanDVO.setTestnubm(testnubm);
        Date sj = ToolsUtils.getDateObject(createdate);
        sanDVO.setCreatedate(sj);
        StringBuffer res = new StringBuffer();
        if(ToolsUtils.isEmpty(qihao)||ToolsUtils.isEmpty(nubm)||ToolsUtils.isEmpty(createdate)){
            res.append("请求数据不能为空");
            return res.toString();
        }
        SanDVO dangQi= sanDDao.QueryDangQi(qihao);
        if (ToolsUtils.notEmpty(dangQi)){
            res.append("已有此数据");
            return res.toString();
        }else {
            sanDDao.saveSand(sanDVO);

        }
        return null;
    }
    /*判断号码奇偶，个十百位必开的情况*/
    public boolean resOddOrOpen (String nubm,QuerySanDReq req) {
        boolean result = true;
        /*百位*/
        if (ToolsUtils.notEmpty(req.getBaiWeiResOdd())){
            if (QueryEnum.ODDJ_FALG.val().equals(req.getBaiWeiResOdd())) { //奇数
                int a = Integer.parseInt(nubm.substring(0,1));
                // String result = (a%2==0)?"偶数":"奇数";
                result = (a%2==0) ? false:true;
                if(!result){
                    return false;
                }
            }
            if (QueryEnum.ODDO_FALG.val().equals(req.getBaiWeiResOdd())) { //偶数
                int a = Integer.parseInt(nubm.substring(0,1));
                result = (a%2==0) ? true:false;
                if(!result){
                    return false;
                }
            }
        }
        /*十位*/
        if(ToolsUtils.notEmpty(req.getShiWeiResOdd())){
            if (QueryEnum.ODDJ_FALG.val().equals(req.getShiWeiResOdd())) { //奇数
                int a = Integer.parseInt(nubm.substring(1,2));
                // String result = (a%2==0)?"偶数":"奇数";
                result = (a%2==0) ? false:true;
                if(!result){
                    return false;
                }
            }
            if (QueryEnum.ODDO_FALG.val().equals(req.getShiWeiResOdd())) { //偶数
                int a = Integer.parseInt(nubm.substring(1,2));
                result = (a%2==0) ? true:false;
                if(!result){
                    return false;
                }
            }
        }
        /*个位*/
        if(ToolsUtils.notEmpty(req.getGeWeiResOdd())){
            if (QueryEnum.ODDJ_FALG.val().equals(req.getGeWeiResOdd())) { //奇数
                int a = Integer.parseInt(nubm.substring(2,3));
                // String result = (a%2==0)?"偶数":"奇数";
                result = (a%2==0) ? false:true;
                if(!result){
                    return false;
                }
            }
            if (QueryEnum.ODDO_FALG.val().equals(req.getGeWeiResOdd())) { //偶数
                int a = Integer.parseInt(nubm.substring(2,3));
                result = (a%2==0) ? true:false;
                if(!result){
                    return false;
                }
            }
        }
        return result;
    }
    /*查询3D对象*/
    public List<SanDChaiFen> selectSanDVo (String qishu)   {
        List<SanDChaiFen> sanDVO =new ArrayList<>();
        if(ToolsUtils.notEmpty(qishu)){
            //查询号码集合
            List<SanDVO> sanDList= sanDDao.QueryMessageSandVo(qishu);
            sanDList.forEach(a->{
                SanDChaiFen sanDChaiFen = new SanDChaiFen();
                sanDChaiFen.setQishu(a.getQihao()+"期");
                sanDChaiFen.setDate(new SimpleDateFormat("yyyy-MM-dd").format(a.getCreatedate()));
                sanDChaiFen.setBaiWei(a.getNubm().substring(0,1));
                sanDChaiFen.setShiWei(a.getNubm().substring(1,2));
                sanDChaiFen.setGeWei(a.getNubm().substring(2,3));
                sanDVO.add(sanDChaiFen);
            });

        }
        return sanDVO;
    }


}
